﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using FlyHorse.LIMS.Model;
using FlyHorse.LIMS.WinApp.LanguageResource;

namespace FlyHorse.LIMS.WinApp.BaseManageForms.StandardSemiForms
{
    public partial class StandardSemiDetail : DevExpress.XtraEditors.XtraForm
    {
        int _option;
        StandardSemi _standardSemi;
        BLL.GoodBll goodsBll = new BLL.GoodBll();
        BLL.TestItemBll testItemBll = new BLL.TestItemBll();
        BLL.SamplingPlaceBll samplingPlaceBll = new BLL.SamplingPlaceBll();
        BLL.StandardSemiBll standardSemiBll = new BLL.StandardSemiBll();
        BLL.ToolBll toolBll = new BLL.ToolBll();
        Action _InitGrid;
        public StandardSemiDetail(Action InitGrid)
        {
            InitializeComponent();
            _option = 1;
            _InitGrid = InitGrid;
        }

        public StandardSemiDetail(StandardSemi standardSemi)
        {
            InitializeComponent();
            _option = 2;
            _standardSemi = standardSemi;
        }

        private void StandardSemiDetail_Load(object sender, EventArgs e)
        {

            if (_option == 1)
            {
                //初始化下拉框
                //goods
                var goodsList = toolBll.GetSemiGoodList();
                this.searchLookUpEdit_goods.Properties.DataSource = goodsList;
                //testItem
                var testItemList = testItemBll.LoadEntities(m => true).ToList();
                this.searchLookUpEdit_TestItemName.Properties.DataSource = testItemList;
                //samplingPlace]\
                var placeList = samplingPlaceBll.LoadEntities(m => true).ToList();
                this.searchLookUpEdit_SamplingPlace.Properties.DataSource = placeList;
            }
            if (_option == 2)
            {
                //初始化下拉框
                //goods
                var goodsList = goodsBll.LoadEntities(m => true).ToList();
                this.searchLookUpEdit_goods.Properties.DataSource = goodsList;
                //testItem
                var testItemList = testItemBll.LoadEntities(m => true).ToList();
                this.searchLookUpEdit_TestItemName.Properties.DataSource = testItemList;
                //samplingPlace]\
                var placeList = samplingPlaceBll.LoadEntities(m => true).ToList();
                this.searchLookUpEdit_SamplingPlace.Properties.DataSource = placeList;

                this.searchLookUpEdit_goods.EditValue = _standardSemi.GoodsID;
                this.searchLookUpEdit_TestItemName.EditValue = _standardSemi.TestItemID;
                this.searchLookUpEdit_SamplingPlace.EditValue = _standardSemi.SamplingPlaceID;
                this.textEdit_minValue.Text = _standardSemi.MinValue.ToString();
                this.textEdit_MaxValue.Text = _standardSemi.MaxValue.ToString();
                this.textEdit_Order.Text = _standardSemi.FormOrder.ToString();
                this.textEdit_Remark.Text = _standardSemi.Remark;

                this.searchLookUpEdit_goods.Enabled = false;
                this.searchLookUpEdit_TestItemName.Enabled = false;
                this.searchLookUpEdit_SamplingPlace.Enabled = false;

                this.Text = ResourceOverall.EditStr;
            }
        }

        private void simpleButton_save_Click(object sender, EventArgs e)
        {
            #region //判空
            if (this.searchLookUpEdit_goods.EditValue == null || this.searchLookUpEdit_goods.EditValue.ToString() == "")
            {
                searchLookUpEdit_goods.Focus();
                XtraMessageBox.Show(ResourceOverall.InputNull);
                return;
            }
            if (this.searchLookUpEdit_TestItemName.EditValue == null)
            {
                searchLookUpEdit_TestItemName.Focus();
                XtraMessageBox.Show(ResourceOverall.InputNull);
                return;
            }
            if (this.searchLookUpEdit_SamplingPlace.EditValue == null)
            {
                searchLookUpEdit_SamplingPlace.Focus();
                XtraMessageBox.Show(ResourceOverall.InputNull);
                return;
            }
            //if (this.textEdit_minValue.Text == "")
            //{
            //    textEdit_minValue.Focus();
            //    XtraMessageBox.Show("最小值必填！");
            //    return;
            //}
            //if (this.textEdit_MaxValue.Text == "")
            //{
            //    textEdit_minValue.Focus();
            //    XtraMessageBox.Show("最大值必填！");
            //    return;
            //}
            if (this.textEdit_Order.Text == "")
            {
                textEdit_Order.Focus();
                XtraMessageBox.Show(ResourceOverall.InputNull);
                return;
            }
            #endregion
          
            if (_option == 1)
            {
                StandardSemi sandard = new StandardSemi();
                sandard.StandardSemiID = Guid.NewGuid().ToString();
                sandard.GoodsID = Convert.ToString(this.searchLookUpEdit_goods.EditValue);
                sandard.Remark = this.textEdit_Remark.Text;
                sandard.SamplingPlaceID = Convert.ToString(this.searchLookUpEdit_SamplingPlace.EditValue);
                sandard.TestItemID = Convert.ToString(this.searchLookUpEdit_TestItemName.EditValue);

                if (this.textEdit_minValue.Text!="")
                {
                    try
                    {
                        Convert.ToDouble(this.textEdit_minValue.Text);
                        sandard.MinValue = this.textEdit_minValue.Text;
                    }
                    catch (Exception ex)
                    {
                        textEdit_minValue.Focus();
                        XtraMessageBox.Show(ex.Message);
                        return;
                    } 
                }
                else
                {
                    sandard.MinValue = null;
                }


                if (textEdit_MaxValue.Text!="")
                {
                    try
                    {
                        Convert.ToDouble(this.textEdit_MaxValue.Text);
                        sandard.MaxValue =this.textEdit_MaxValue.Text;
                    }
                    catch (Exception ex)
                    {
                        textEdit_MaxValue.Focus();
                        XtraMessageBox.Show(ex.Message);
                        return;
                    } 
                }
                else
                {
                    sandard.MaxValue = null;
                }


                try
                {
                    sandard.FormOrder = Convert.ToInt32(this.textEdit_Order.Text);
                }
                catch (Exception ex)
                {
                    XtraMessageBox.Show(ex.Message);
                    textEdit_Order.Focus();
                    return;
                }

                #region //判重 判大小
                int count = standardSemiBll.LoadEntitiesAsNoTracking(m => m.GoodsID == sandard.GoodsID && m.TestItemID == sandard.TestItemID && m.SamplingPlaceID == sandard.SamplingPlaceID).Count();
                if (count>0)
                {
                    XtraMessageBox.Show(ResourceOverall.DataExists);
                    return;
                }
                if (sandard.MinValue!=null && sandard.MaxValue!=null)
                {
                    if (Convert.ToDouble( sandard.MinValue)>=Convert.ToDouble(sandard.MaxValue))
                    {
                        XtraMessageBox.Show(ResourceOverall.ValueCompareErr);
                         return;
                    }
                }
                #endregion

                using (OJCB4Entities db =new OJCB4Entities())
                {
                    db.StandardSemis.Add(sandard);
                    var res = db.SaveChanges() > 0;
                    if (res)
                    {
                        _InitGrid();
                        XtraMessageBox.Show(ResourceOverall.CreateSucceed);
                        this.Close();
                        return;
                    }
                }
            }

            if (_option == 2)
            {
                _standardSemi.GoodsID = Convert.ToString(this.searchLookUpEdit_goods.EditValue);
                _standardSemi.Remark = this.textEdit_Remark.Text;
                _standardSemi.SamplingPlaceID = Convert.ToString(this.searchLookUpEdit_SamplingPlace.EditValue);
                _standardSemi.TestItemID = Convert.ToString(this.searchLookUpEdit_TestItemName.EditValue);

                if (this.textEdit_minValue.Text != "")
                {
                    try
                    {
                        Convert.ToDouble(this.textEdit_minValue.Text);
                        _standardSemi.MinValue = this.textEdit_minValue.Text;
                    }
                    catch (Exception ex)
                    {
                        textEdit_minValue.Focus();
                        XtraMessageBox.Show(ex.Message);
                        return;
                    }
                }
                else
                {
                    _standardSemi.MinValue = null;
                }


                if (textEdit_MaxValue.Text != "")
                {
                    try
                    {
                        Convert.ToDouble(this.textEdit_MaxValue.Text);
                        _standardSemi.MaxValue = this.textEdit_MaxValue.Text;
                    }
                    catch (Exception ex)
                    {
                        textEdit_MaxValue.Focus();
                        XtraMessageBox.Show(ex.Message);
                        return;
                    }
                }
                else
                {
                    _standardSemi.MaxValue = null;
                }

                try
                {
                    _standardSemi.FormOrder = Convert.ToInt32(this.textEdit_Order.Text);
                }
                catch (Exception ex)
                {
                    textEdit_Order.Focus();
                    XtraMessageBox.Show(ex.Message);
                    return;
                }

                #region //判大小
 
                if (_standardSemi.MinValue != null && _standardSemi.MaxValue != null)
                {
                    if (Convert.ToDouble( _standardSemi.MinValue )>=Convert.ToDouble( _standardSemi.MaxValue))
                    {
                        XtraMessageBox.Show(ResourceOverall.ValueCompareErr);
                        return;
                    }
                }
                #endregion

                bool res=  standardSemiBll.EditEntity(_standardSemi);
                if (res)
                {
                    XtraMessageBox.Show(ResourceOverall.EditSucceed);
                }
               
            }

        }
    }
}